//@Time:2022/4/8 19:25
//@Author:aFun

package indi.csms_springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import indi.csms_springboot.domain.*;
import indi.csms_springboot.service.TeacherService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacherController")
public class TeacherController {

    @Autowired
    TeacherService teacherService;
    // ========================================================================================
    // 查询课程信息模块
    @RequestMapping("/findCourseByPage")
    public ModelAndView findCourserByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        User user = (User) request.getSession().getAttribute("user");
        Integer teaId = teacherService.findTeaIdByAccount(user);
        Map<String, String[]> condition = request.getParameterMap();
        IPage<Course> pb = teacherService.findCourseByPage(currentPage, rows, condition, teaId);
        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);
        modelAndView.setViewName("teacherList");
        return modelAndView;
    }

    @RequestMapping("/addCourse")
    public ModelAndView addCourse(HttpServletRequest request) throws UnsupportedEncodingException, InvocationTargetException, IllegalAccessException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        Course course = new Course();
        BeanUtils.populate(course, map);
        boolean b = teacherService.addCourse(course);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/teacherController/findCourseByPage");
        return modelAndView;
    }

    @RequestMapping("/findCourse")
    public ModelAndView findCourse(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        String couId = request.getParameter("couId");
        Course course = teacherService.findCourseByCouId(Integer.parseInt(couId));
        request.setAttribute("course", course);
        modelAndView.setViewName("updateCourse");
        return modelAndView;
    }

    @RequestMapping("/updateCourse")
    public ModelAndView updateCourse(HttpServletRequest request) throws UnsupportedEncodingException, InvocationTargetException, IllegalAccessException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        Course course = new Course();
        BeanUtils.populate(course, map);
        boolean b = teacherService.updateCourse(course);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/teacherController/findCourseByPage");
        return modelAndView;
    }

    @RequestMapping("/deleteCourse")
    public ModelAndView deleteCourse(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        String couId = request.getParameter("couId");
        Course course = new Course();
        course.setCouId(Integer.parseInt(couId));
        boolean b = teacherService.deleteCourse(course);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/teacherController/findCourseByPage");
        return modelAndView;
    }

    @RequestMapping("/deleteCourseSelected")
    public ModelAndView deleteCourseSelected(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        String[] ids = request.getParameterValues("couId");
        boolean b = teacherService.deleteSelected(ids);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/teacherController/findCourseByPage");
        return modelAndView;
    }

    // ========================================================================================



    // ========================================================================================
    // 我的学生信息模块
    @RequestMapping("/findStudentByPage")
    public ModelAndView findStudentByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        Map<String, String[]> condition = request.getParameterMap();
        User user = (User) request.getSession().getAttribute("user");
        Integer teaId = teacherService.findTeaIdByAccount(user);
        IPage<StudentOrder> pb = teacherService.findStudentByPage(currentPage, rows, condition, teaId);
        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);
        modelAndView.setViewName("teacherPrintStuOrder");
        return modelAndView;
    }

    @RequestMapping("/recordScore")
    public ModelAndView recordScore(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();

        request.setCharacterEncoding("utf-8");

        Map<String, String[]> maps = request.getParameterMap();
        String[] ssIds = maps.get("ssId");
        String[] scores = maps.get("score");

        List<SSFTVO> SSFTVOList =new ArrayList<>();
        for (int i = 0; i < ssIds.length; i += 1) {
            SSFTVO ss = new SSFTVO();
            ss.setSsId(Integer.parseInt(ssIds[i]));
            ss.setScore(Integer.parseInt(scores[i]));
            SSFTVOList.add(ss);
        }
        boolean b = teacherService.recordScore(SSFTVOList);


        modelAndView.setViewName("redirect:"+request.getContextPath()+"/teacherController/findStudentByPage");
        return modelAndView;
    }
    // ========================================================================================

    // ========================================================================================
    // 选课模块
    @RequestMapping("/findCourseToSelect")
    public ModelAndView findCourseToSelect(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        User user = (User) request.getSession().getAttribute("user");
        Map<String, String[]> condition = request.getParameterMap();

        Integer teaId = teacherService.findTeaIdByAccount(user);
        IPage<Course> pb = teacherService.findImOpenCourse(currentPage, rows, condition,teaId);


        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);
        request.setAttribute("teaId", teaId);

        modelAndView.setViewName("teacherSelectCourse");

        return modelAndView;
    }


    @RequestMapping("/teacherSelect")
    public ModelAndView teacherSelect(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        String couId = request.getParameter("couId");
        String teaId = request.getParameter("teaId");
        Course course = new Course();
        course.setCouId(Integer.parseInt(couId));
        Teacher teacher = new Teacher();
        teacher.setTeaId(Integer.parseInt(teaId));
        boolean b = teacherService.selectCourse(course, teacher);
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/teacherController/findCourseToSelect");
        return modelAndView;
    }
    // ========================================================================================
}
